package com.jar.exercise.io.字节流;

import org.junit.Test;

import java.io.*;

/**
 * 使用字节流copy文件，
 * 普通的FileInputStream和加了缓冲区的BufferedInputStream
 */
public class CopyFile {
	@Test
	public void buffer() throws Exception {
		long l = System.currentTimeMillis();
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:/后台.rar"));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:/后台2.rar"));
		int len;
		while ((len = bis.read()) != -1) {
			bos.write(len);
		}
		bis.close();
		bos.close();
		System.out.println(System.currentTimeMillis() - l + "毫秒");
	}

	@Test
	public void noBuffer() throws Exception {
		long l = System.currentTimeMillis();
		FileInputStream bis = new FileInputStream("d:/后台.rar");
		FileOutputStream bos = new FileOutputStream("d:/后台2.rar");
		byte[] b = new byte[1024];
		int len;
		while ((len = bis.read(b)) != -1) {
			bos.write(b, 0, len);
		}
		bis.close();
		bos.close();
		System.out.println(System.currentTimeMillis() - l + "毫秒");
	}
}
